package com.jfzt;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @author: shunpeng.hu
 * @date: 2024/2/1 16:39
 */
@Slf4j
@SpringBootTest
public class SpringBootTest03 {

    private ElasticsearchClient client;

    private ElasticsearchAsyncClient asyncClient;

    private ElasticsearchTransport transport;

    public static final String INDEX_CONSTANT = "loginv2";

    @BeforeEach
    void setUp() {
        try {
            RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));

            RestClient restClient = builder.build();
            this.transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

            //获取同步客户端
            this.client = new ElasticsearchClient(transport);
            //获取异步客户端
            this.asyncClient = new ElasticsearchAsyncClient(transport);

            //关闭通道
//            transport.close();
        } catch (Exception e) {
            log.error("ES客户端连接异常~");
            throw new RuntimeException("ES客户端连接异常~", e);
        }
    }

    @Test
    @DisplayName(value = "ES索引CRUD操作")
    void createIndex() {
        try {

            //客户端对象
            final ElasticsearchIndicesClient indices = client.indices();

            //判断索引是否存在
            ExistsRequest existsRequest = new ExistsRequest.Builder().index(INDEX_CONSTANT).build();
            boolean flag = true;
            flag = indices.exists(existsRequest).value();
            if (!flag) {
                //创建索引
                CreateIndexRequest request = new CreateIndexRequest.Builder().index(INDEX_CONSTANT).build();
                CreateIndexResponse createIndexResponse = indices.create(request);
                log.info("创建索引库成功~", createIndexResponse);
            }

            //查询索引
            GetIndexRequest getIndexRequest = new GetIndexRequest.Builder().index(INDEX_CONSTANT).build();
            GetIndexResponse getIndexResponse = indices.get(getIndexRequest);
            log.info("查询索引库成功~", getIndexResponse);

            //删除索引
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(INDEX_CONSTANT).build();
            DeleteIndexResponse deleteIndexResponse = indices.delete(deleteIndexRequest);
            log.info("删除索引库成功~", deleteIndexResponse.acknowledged());

            //关闭通道
            transport.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
